public class MainWnd : Gtk.Window 
{
	private weak DocumentManager document_manager = DocumentManager.get_instance ();
	private weak UIManager ui_manager = UIManager.get_instance ();

	protected virtual void on_file_new_actived ()
	{
		document_manager.create (null);
	}
	
	protected virtual void on_file_quit_actived ()
	{
		if (document_manager.close_all ())
		{
			this.destroy ();
		}
	}
	
	protected virtual bool on_delete_event ()
	{
		return (!document_manager.close_all ());
	}
	
	construct 
	{
		this.title = "FootEdit";
		this.resize (800, 600);
		this.destroy += Gtk.main_quit;
		
		Gtk.VBox vbox = new Gtk.VBox (false, 0);
		this.add (vbox);

		Gtk.Widget menubar = ui_manager.get_widget ("/main_menu");
		if (menubar != null)
		{
			/*Obsługa akcji*/
    			ui_manager.get_action("file_new").activate		+= this.on_file_new_actived;		
			ui_manager.get_action("file_open").activate       	+= document_manager.open;
			ui_manager.get_action("file_save").activate       	+= document_manager.save_current;
			ui_manager.get_action("file_save_as").activate    	+= document_manager.save_as_current;
			ui_manager.get_action("file_save_all").activate   	+= document_manager.save_all;
			ui_manager.get_action("file_close").activate      	+= document_manager.close_current;
			ui_manager.get_action("file_close_all").activate  	+= () => {
											document_manager.close_all ();	
										};
			ui_manager.get_action("file_quit").activate       	+= this.on_file_quit_actived;

			ui_manager.get_action("edit_undo").activate		+= document_manager.undo_current;
			ui_manager.get_action("edit_redo").activate		+= document_manager.redo_current;
			ui_manager.get_action("edit_cut").activate		+= document_manager.cut_current;
			ui_manager.get_action("edit_copy").activate		+= document_manager.copy_current;
			ui_manager.get_action("edit_paste").activate		+= document_manager.paste_current;
			ui_manager.get_action("edit_delete").activate		+= document_manager.delete_current;
			ui_manager.get_action("edit_select_all").activate	+= document_manager.select_all_current;
			ui_manager.set_sensitive_doc_actions (false);

			ui_manager.get_action("search_find").activate		+= document_manager.search_in_current;
			ui_manager.get_action("search_find_next").activate	+= document_manager.goto_next_found_match_current;
			ui_manager.get_action("search_find_prev").activate	+= document_manager.goto_prev_found_match_current;
			ui_manager.get_action("search_replace").activate	+= document_manager.replace_in_current;
			ui_manager.get_action("search_goto_line").activate 	+= document_manager.goto_line_current;
			
			vbox.pack_start (menubar, false, false, 0);
			
			this.add_accel_group (ui_manager.get_accel_group ());
		}
		else
		{
			var msg = new Gtk.MessageDialog (
	        		null, Gtk.DialogFlags.MODAL, Gtk.MessageType.ERROR, Gtk.ButtonsType.OK, 
			        "Nie moge pobrac menu!"
			);
    			msg.run ();
    			msg.destroy ();
		}
		
		vbox.pack_start_defaults (
			DocumentManager.get_instance ()
		);
		
		this.delete_event += this.on_delete_event;
	}

	public static int main (string[] args)
	{
		Gtk.init (ref args);
		
		var wnd = new MainWnd ();
		wnd.show_all ();
		if (args.length > 1)
		{
			for (int i = 1; i < args.length; ++i) //Otwieramy pliki podane jako argument funkcji...
			{
				DocumentManager.get_instance().create (args[i]);
			}					
		}
		else 
		{
			DocumentManager.get_instance().create (null);
		}
		Gtk.main ();
		return 0;
	}
}
